package com.jilujia.framework.security;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * <p>
 * <security:authentication-manager> <security:authentication-provider user-service-ref="customUserDetailsService">
 * <security:password-encoder ref="passwordEncoder" /> </security:authentication-provider>
 * </security:authentication-manager>
 * </p>
 * <p>
 * <bean id="customUserDetailsService" class="com.jilujia.framework.security.JilujiaUserDetailsService"> <property
 * name="dataSource" ref="dataSource" /> </bean>
 * </p>
 * 
 * @author john.zhou
 * @version $Id: JilujiaUserDetailsService.java 88 2012-06-15 07:08:49Z catton2000@gmail.com $
 * @notes Created on May 30, 2012<br>
 *        Revision of last commit:$Revision: 88 $<br>
 *        Author of last commit:$Author: catton2000@gmail.com $<br>
 *        Date of last commit:$Date: 2012-06-15 07:08:49 +0000 (Fri, 15 Jun 2012) $<br>
 *        <p>
 */
public class JilujiaUserDetailsService implements UserDetailsService {

	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		String sql = "SELECT U.username , U.password, U.nickname, U.id, U.enable as 'enabled' FROM TB_USER U where U.username=:username and U.active=1";
		MapSqlParameterSource source = new MapSqlParameterSource();
		source.addValue("username", username);

		NamedParameterJdbcTemplate sjt = new NamedParameterJdbcTemplate(dataSource);
		User user = sjt.queryForObject(sql, source, new UserMapper());
		return user;
	}

	private List<GrantedAuthority> getAuthorities(boolean isAdmin) {
		ArrayList<GrantedAuthority> dummy = new ArrayList<GrantedAuthority>();
		dummy.add(new GrantedAuthorityImpl("ROLE_USER"));
		return dummy;
	}

	private class UserMapper implements RowMapper<User> {

		public User mapRow(ResultSet rs, int arg1) throws SQLException {
			JilujiaUser my = new JilujiaUser(rs.getString("username"), rs.getString("password"), rs.getLong("id"), rs
					.getString("nickname"), rs.getBoolean("enabled"), true, true, true, getAuthorities(true));
			return my;
		}
	}
}
